<template>
  <div class="config-group">
    <h4>尾部组件配置</h4>
    
    <!-- 菜单配置 -->
    <div class="config-section">
      <div class="section-title">菜单配置</div>
      <div class="menu-items">
        <div v-for="(item, index) in localConfig.menuItems" :key="index" class="menu-item-config">
          <div class="menu-item-inputs">
            <div class="input-group">
              <label>菜单名称</label>
              <input 
                v-model="item.text"
                placeholder="请输入菜单名称"
                @input="updateConfig"
                class="menu-text-input"
              >
            </div>
            <div class="input-group">
              <label>跳转链接</label>
              <input 
                v-model="item.route"
                placeholder="请输入跳转链接"
                @input="updateConfig"
                class="menu-route-input"
              >
            </div>
          </div>
          <button @click="removeMenuItem(index)" class="remove-btn">×</button>
        </div>
      </div>
      <button @click="addMenuItem" class="add-btn">
        <span class="btn-icon">+</span>
        添加菜单项
      </button>
    </div>

    <!-- 样式配置 -->
    <div class="config-section">
      <div class="section-title">样式设置</div>
      <div class="config-row">
        <div class="config-item flex-item">
          <label>菜单颜色：</label>
          <el-color-picker 
            v-model="localConfig.menuColor"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
        <div class="config-item flex-item">
          <label>字体大小：</label>
          <input 
            type="text" 
            v-model="localConfig.menuFontSize"
            @input="updateConfig"
            placeholder="例如：16px"
          >
        </div>
        <div class="config-item flex-item">
          <label>层级：</label>
          <input 
            type="number" 
            v-model.number="localConfig.zIndex" 
            @input="updateConfig"
            placeholder="例如：1"
            min="0"
          >
        </div>
      </div>
    </div>

    <!-- 背景配置 -->
    <div class="config-section">
      <div class="section-title">背景设置</div>
      
      <!-- 背景图片配置 -->
      <div class="bg-image-config">
        <div class="preview-area">
          <div class="preview-box" :style="previewStyle">
            <span v-if="!localConfig.backgroundImage">暂无图片</span>
          </div>
        </div>
        <div class="image-controls">
          <div class="image-path">
            <span class="path-label">已选图片：</span>
            <span class="path-value">{{ getImageName(localConfig.backgroundImage) || '未选择' }}</span>
          </div>
          <div class="image-actions">
            <ImageSelector 
              v-model="localConfig.backgroundImage"
              @change="handleImageChange"
            />
          </div>
          <div class="image-tips">
            <span class="tips-icon">💡</span>
            <span>图片位于 assets/images 目录下</span>
          </div>
        </div>
      </div>

      <!-- 背景颜色和高度配置 -->
      <div class="config-row">
        <div class="config-item flex-item">
          <label>背景颜色：</label>
          <el-color-picker 
            v-model="localConfig.backgroundColor"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
        <div class="config-item flex-item">
          <label>高度：</label>
          <input 
            type="text" 
            v-model="localConfig.height"
            @input="updateConfig"
            placeholder="例如：120px"
          >
        </div>
      </div>
    </div>

    <!-- 菜单样式设置 -->
    <div class="config-section">
      <div class="section-title">菜单样式设置</div>
      <div class="config-row">
        <div class="config-item flex-item">
          <label>默认文字颜色：</label>
          <el-color-picker 
            v-model="localConfig.menuColor"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
        <div class="config-item flex-item">
          <label>选中文字颜色：</label>
          <el-color-picker 
            v-model="localConfig.menuActiveColor"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
      </div>

      <div class="config-row">
        <div class="config-item flex-item">
          <label>默认背景色：</label>
          <el-color-picker 
            v-model="localConfig.menuBackground"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
        <div class="config-item flex-item">
          <label>选中背景色：</label>
          <el-color-picker 
            v-model="localConfig.menuActiveBackground"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
      </div>

      <div class="config-row">
        <div class="config-item flex-item">
          <label>菜单项宽度：</label>
          <input 
            type="text" 
            v-model="localConfig.menuWidth"
            @input="updateConfig"
            placeholder="例如：100px"
          >
        </div>
        <div class="config-item flex-item">
          <label>菜单项高度：</label>
          <input 
            type="text" 
            v-model="localConfig.menuHeight"
            @input="updateConfig"
            placeholder="例如：40px"
          >
        </div>
      </div>

      <!-- 菜单背景图片设置 -->
      <div class="menu-bg-config">
        <div class="config-item-title">默认状态背景图：</div>
        <div class="bg-image-config">
          <div class="preview-area">
            <div 
              class="preview-box" 
              :style="{
                backgroundImage: localConfig.menuBackgroundImage ? `url(${localConfig.menuBackgroundImage})` : 'none',
                backgroundColor: localConfig.menuBackgroundImage ? 'transparent' : '#f5f5f5',
                height: '60px'
              }"
            >
              <span v-if="!localConfig.menuBackgroundImage">暂无图片</span>
            </div>
          </div>
          <div class="image-controls">
            <ImageSelector 
              v-model="localConfig.menuBackgroundImage"
              @change="handleMenuBgImageChange"
            />
          </div>
        </div>
      </div>

      <!-- 选中状态背景图片 -->
      <div class="menu-bg-config">
        <div class="config-item-title">选中状态背景图：</div>
        <div class="bg-image-config">
          <div class="preview-area">
            <div 
              class="preview-box" 
              :style="{
                backgroundImage: localConfig.menuActiveBackgroundImage ? `url(${localConfig.menuActiveBackgroundImage})` : 'none',
                backgroundColor: localConfig.menuActiveBackgroundImage ? 'transparent' : '#f5f5f5',
                height: '60px'
              }"
            >
              <span v-if="!localConfig.menuActiveBackgroundImage">暂无图片</span>
            </div>
          </div>
          <div class="image-controls">
            <ImageSelector 
              v-model="localConfig.menuActiveBackgroundImage"
              @change="handleMenuActiveBgImageChange"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- 当前选中菜单 -->
    <div class="config-section">
      <div class="section-title">当前选中菜单</div>
      <div class="config-row">
        <div class="config-item flex-item">
          <label>选中菜单：</label>
          <select 
            v-model="localConfig.currentMenu"
            @change="updateConfig"
            class="menu-select"
          >
            <option value="">无</option>
            <option 
              v-for="(item, index) in localConfig.menuItems"
              :key="index"
              :value="item.text"
            >
              {{ item.text }}
            </option>
          </select>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ImageSelector from '../../common/ImageSelector.vue'

export default {
  name: 'CommonFooterConfig',
  components: {
    ImageSelector
  },
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      localConfig: {
        menuItems: [],
        menuColor: '#ffffff',
        menuActiveColor: '#1890ff',
        menuFontSize: '16px',
        menuBackground: 'transparent',
        menuActiveBackground: 'rgba(255, 255, 255, 0.1)',
        menuWidth: 'auto',
        menuHeight: '40px',
        backgroundColor: 'unset',
        backgroundImage: '',
        height: '120px',
        zIndex: 1,
        menuBackgroundImage: '',        // 默认背景图片
        menuActiveBackgroundImage: '',  // 选中状态背景图片
      },
      predefineColors: [
        '#ffffff',
        '#000000',
        '#409EFF',
        '#67C23A',
        '#E6A23C',
        '#F56C6C',
        '#909399',
        'rgba(255, 255, 255, 0.7)',
        'rgba(0, 0, 0, 0.7)'
      ]
    }
  },
  created() {
    this.localConfig = { ...this.config }
  },
  methods: {
    updateConfig() {
      this.$emit('update:config', { ...this.localConfig })
    },
    addMenuItem() {
      this.localConfig.menuItems.push({ text: '新菜单项', route: '/' })
      this.updateConfig()
    },
    removeMenuItem(index) {
      this.localConfig.menuItems.splice(index, 1)
      this.updateConfig()
    },
    handleImageChange(imagePath) {
      this.localConfig.backgroundImage = imagePath
      this.updateConfig()
    },
    getImageName(path) {
      if (!path) return '';
      return path.split('/').pop();
    },
    handleMenuBgImageChange(imagePath) {
      this.localConfig.menuBackgroundImage = imagePath;
      this.updateConfig();
    },
    handleMenuActiveBgImageChange(imagePath) {
      this.localConfig.menuActiveBackgroundImage = imagePath;
      this.updateConfig();
    }
  },
  computed: {
    previewStyle() {
      return {
        backgroundImage: this.localConfig.backgroundImage ? `url(${this.localConfig.backgroundImage})` : 'none',
        backgroundColor: this.localConfig.backgroundImage ? 'transparent' : '#f5f5f5'
      }
    }
  }
}
</script>

<style scoped>
/* 复用 CommonHeaderConfig 的基础样式 */
.config-group {
  margin-bottom: 20px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 6px;
}

.config-group h4 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 14px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.config-section {
  margin-bottom: 20px;
  padding: 15px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.section-title {
  font-size: 13px;
  color: #333;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px dashed #eee;
}

/* 菜单项配置样式 */
.menu-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.menu-item-config {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  background: #f9f9f9;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #eee;
}

.menu-item-inputs {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.input-group label {
  font-size: 12px;
  color: #666;
}

.menu-text-input,
.menu-route-input {
  width: 100%;
  padding: 4px 8px;
  height: 28px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 12px;
}

.menu-route-input {
  color: #666;
}

.remove-btn {
  background: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  width: 24px;
  height: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 16px; /* 调整删除按钮的垂直位置，与第一个输入框对齐 */
}

.add-btn {
  width: 100%;
  padding: 6px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.btn-icon {
  font-size: 14px;
  font-weight: bold;
}

.add-btn:hover {
  background: #40a9ff;
}

/* 复用 CommonHeaderConfig 的其他样式 */
.config-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

.config-item.flex-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 180px;
  flex: 1;
}

.config-item.flex-item label {
  width: auto;
  min-width: 70px;
  white-space: nowrap;
  color: #666;
  font-size: 13px;
}

.config-item.flex-item input {
  flex: 1;
  min-width: 80px;
  padding: 4px 8px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 13px;
}

/* 图片预览相关样式 */
.bg-image-config {
  margin-bottom: 15px;
  background: #f9f9f9;
  border-radius: 4px;
  padding: 12px;
}

.preview-area {
  margin-bottom: 12px;
}

.preview-box {
  width: 100%;
  height: 120px;
  border: 2px dashed #ddd;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #999;
  font-size: 13px;
}

.image-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.image-path {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.path-label {
  color: #666;
  white-space: nowrap;
}

.path-value {
  color: #1890ff;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-tips {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #999;
  font-size: 12px;
}

.tips-icon {
  font-size: 14px;
}

/* Element UI 组件样式覆盖 */
:deep(.el-color-picker__trigger) {
  height: 32px;
  width: 60px;
  border: 1px solid #ddd;
  padding: 2px;
}

/* 添加滚动条样式 */
.menu-items-container::-webkit-scrollbar {
  width: 6px;
}

.menu-items-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.menu-items-container::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 3px;
}

.menu-items-container::-webkit-scrollbar-thumb:hover {
  background: #999;
}

/* 添加新的样式 */
.menu-bg-config {
  margin-bottom: 20px;
}

.config-item-title {
  font-size: 13px;
  color: #666;
  margin-bottom: 8px;
}

.preview-box {
  width: 100%;
  border: 2px dashed #ddd;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #999;
  font-size: 13px;
  margin-bottom: 8px;
}

/* 添加选择器样式 */
.menu-select {
  flex: 1;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 8px;
  color: #666;
  font-size: 13px;
  background-color: #fff;
}

.menu-select:focus {
  border-color: #1890ff;
  outline: none;
}
</style> 